#!/usr/bin/env node

const fs = require('fs'),
      http = require('http'),
      data = require('./data.js');

let dataList = data.filter(val=>{
  if(val.movieRange !== '暂无评分'){
    return true;
  }
});
dataList.sort((a, b)=>{
  return b.movieRange - a.movieRange;
});
if(!fs.existsSync('./index.html')){
  console.error('index.html不存在！');
  process.exit(1);                                                                                                   
}
const html = fs.readFileSync('./index.html');
http.createServer((req, res)=>{
  if(req.url === '/'){
    res.end(html);
  }else if(req.url === '/getlist'){
    res.end(JSON.stringify(dataList));
  }else{
    res.statusCode = 404;
    res.end(`<h1>NOT FOUND</h1>`);
  }
}).listen(8080);
